package com.yzyz.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yzyz.bean.scenic.DescriptionBean;
import com.yzyz.bean.scenic.ScenicBean;
import com.yzyz.dao.AttractionsDao;
import com.yzyz.dao.DescriptionDao;
import com.yzyz.dao.PictureDao;
import com.yzyz.dao.IScenicDao;
import com.yzyz.service.IRouteService;
import com.yzyz.service.ScenicService;
import com.yzyz.utils.error.Error;

/**
 * 
 * @author David Sun
 * @Date   2010-3-28
 *
 */
@Transactional
public class ScenicServiceImpl implements ScenicService {
	private IScenicDao scenicDao;
	private DescriptionDao descriptionDao;
	private AttractionsDao attractionsDao;
	private PictureDao pictureDao;
	private IRouteService routeService;
	
	public void setRouteService(IRouteService routeService) {
		this.routeService = routeService;
	}

	public void setPictureDao(PictureDao pictureDao) {
		this.pictureDao = pictureDao;
	}

	public void setAttractionsDao(AttractionsDao attractionsDao) {
		this.attractionsDao = attractionsDao;
	}

	public void setDescriptionDao(DescriptionDao descriptionDao) {
		this.descriptionDao = descriptionDao;
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int add(Object object) {
		int scenicId = this.scenicDao.save(object);
		((ScenicBean)object).setId(scenicId);
		return Error.SUCCESS;
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int delete(int id) {
		if(this.scenicDao.delete(id) != 1){
			return Error.OBJECT_NOT_EXIST;
		} 
		return Error.SUCCESS;
	}

	@Override
	public Object find(int id) {
		return this.scenicDao.get(id);
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int update(Object object) {
		this.scenicDao.update(object);
		return Error.SUCCESS;
	}

	@Override
	public void setScenicDao(IScenicDao scenicDao) {
		this.scenicDao = scenicDao;
		
	}

	@Override
	public List<ScenicBean> listAllWithoutDisabled() {
		return this.scenicDao.getAllWithoutDisabled();
	}

	@Override
	public List<ScenicBean> listAllWithouDisabled(int scenicId) {
		
		return null;
	}

	@Override
	public List<ScenicBean> paginate(int start, int limit, String name, int groupId) {
		if(start < 0) {
			start = 0;
		}
		if(limit < 0) {
			limit = 15;
		}
		return this.scenicDao.paginate(start, limit, name, groupId);
	}

	@Override
	public int getCountAll(String name, int groupId) {
		
		return this.scenicDao.getCountAll(name, groupId);
	}

	@Override
	public List<ScenicBean> listAllByRegion(int regionId, int start, int limit) {
		
		return this.scenicDao.getAllByRegion(regionId, start, limit);
	}

	@Override
	public List<ScenicBean> listUsed(int routeId) {
		
		return this.scenicDao.getUsed(routeId);
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int add(ScenicBean scenic, String description) {
		int scenicId = this.scenicDao.save(scenic);
		DescriptionBean des = new DescriptionBean();
		des.setContents(description);
		des.setOwnerId(scenicId);
		des.setDate(new Date());
		des.setName(scenic.getName());
		des.setId(this.descriptionDao.save(des));
		scenic.setId(scenicId);
		scenic.setDescription(des);
		return Error.SUCCESS;
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int updateAvailable(int id, boolean available) {
		this.scenicDao.updateAvailable(id, available);
		return Error.SUCCESS;
	}

	@Override
	public int getCountByRegion(int regionId) {
		
		return this.scenicDao.getCountByRegion(regionId);
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int addUsedScenic(int routeId, int scenicId) {
		this.scenicDao.addUsedScenic(routeId, scenicId);
		return Error.SUCCESS;
	}

	@Override
	public int cancelUsedScenic(int routeId, int scenicId) {
		this.scenicDao.deleteUsedScenic(routeId, scenicId);
		return Error.SUCCESS;
	}

	@Override
	public List<ScenicBean> init(int regionId) {
		List<ScenicBean> scenics = this.scenicDao.getAllByRegion(regionId, 0, 1000);
		if(scenics != null){
			for(ScenicBean scenic: scenics){
				System.out.println(scenic.getName());
				scenic.setAttractions(this.attractionsDao.paginate(scenic.getId(), 0, 1000));
				scenic.setDescription(this.descriptionDao.getByScenic(scenic.getId()));
				scenic.setPictures(this.pictureDao.getPicturesWithoutContents(scenic.getId()));
				scenic.setRoutes(this.routeService.listAllByScenic(scenic.getId()));
			}
		}
		return scenics;
	}

	@Override
	public List<ScenicBean> search(String keyWords) {
		
		return this.scenicDao.search(keyWords);
	}

	@Override
	public ScenicBean initSingle(int scenicId) {
		ScenicBean scenic = (ScenicBean)this.scenicDao.get(scenicId);
		if(scenic != null){
			scenic.setRoutes(this.routeService.listAllByScenic(scenicId));
		}
		return scenic;
	}

	@Override
	public ScenicBean findScenicByName(String name) {
		return this.scenicDao.get(name.trim());
	}

	@Override
	public List<?> paginate(int start, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

}
